/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-12-21 22:22:51
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-24 02:21:30
 * @FilePath: \vue3-ts-vite\src\store\modules\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import piniaPersist from '../piniaPersist'
import { setToken, removeToken } from '@/utils/cookies'
import { ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { setStorage,clearStorage } from '@/utils/storage'
import { login, getUserInfo, getPermBtm } from '@/api/login'
const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: '',
      userId:'',
      avatar: '',
      userName:'',
      userInfo: {},
      permissions: [],
      permBtn: [],
      permCode: [],
    }
  },
  getters: {},
  actions: {
   async Login(userInfo: any) {
    return new Promise((resove,reject) => {
      login(userInfo).then((res:any) => {
        resove(res)
        if(res.code == 200){
          const data = res.data
          setToken(data.token)
          this.token = data.token
          this.userInfo = data.dataValues
          this.avatar = data.dataValues.avatar
          this.userName = data.dataValues.userName
          setStorage('userInfo', data.dataValues)
        } 
      })
    })
    },
    // 前端退出
    FedLogOut() {
      clearStorage()
      removeToken()
      this.token = ''
      this.userInfo = {}
      this.permissions = []
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage,
      }
    ]
  }
})

export default useUserStore

function reject(error: any) {
  throw new Error('Function not implemented.')
}
